/*
 * Copyright 2013 Glassy Cinema.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hu.glassy.web.page.account.registration;

import hu.glassy.core.model.Account;
import hu.glassy.web.auth.ApplicationSession;
import hu.glassy.web.page.common.PublicPage;
import hu.glassy.web.page.home.PublicHomePage;
import hu.glassy.web.page.home.UserHomePage;
import hu.glassy.web.panel.account.registration.ActivationPanel;
import hu.glassy.web.panel.common.GlassyPanel;
import org.apache.wicket.Component;
import org.apache.wicket.RestartResponseAtInterceptPageException;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;

/**
 * Ez az oldal jelenik meg, ha a felhasználó nem aktiválta az accountját.
 * Amennyiben a felhasználó fiókja aktív, átirányít a felhasználói kezdőoldalra.
 *
 * @author molnar.alexander@sonrisa.hu
 */
public class AccountActivationPage extends PublicPage
{

  private static final long serialVersionUID = 5927371790023665570L;

  /**
   * Konstruktor
   *
   * @param parameters
   */
  public AccountActivationPage(final PageParameters parameters)
  {
    super(parameters);

    final IModel<Account> model = ApplicationSession.get().getAccount();
    if (model == null || model.getObject() == null)
    {
      if (logger.isDebugEnabled())
      {
        logger.debug("Logged in account not found, redirecting to public home page...");
      }
      throw new RestartResponseAtInterceptPageException(PublicHomePage.class);
    }

    if (model.getObject().isActive())
    {
      if (logger.isDebugEnabled())
      {
        logger.debug("Logged in account is active, redirecting to user home page...");
      }
      throw new RestartResponseAtInterceptPageException(UserHomePage.class);
    }
  }

  @Override
  public Panel getContentPanel(final String panelId, final PageParameters parameters)
  {
    return new GlassyPanel<Account>(panelId)
    {
      private static final long serialVersionUID = 1L;

      @Override
      public IModel<String> getTitle()
      {
        return new ResourceModel("activation.title");
      }

      @Override
      public Component getContent(final String componentId)
      {
        return new ActivationPanel(componentId, ApplicationSession.get().getAccount());
      }
    };
  }

}
